package com.sanpu.kxe.business.repay.presenter;

import com.sanpu.kxe.business.repay.contract.RepaySupportContract;
import com.sanpu.kxe.business.trade.bean.SupportBankOutput;
import com.sanpu.kxe.business.trade.provider.ITradeProvider;
import com.sanpu.kxe.utils.PublicProviderUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：可支持银行
 * Created by 薛勇军 on 2019/4/26.
 */

public class RepaySupportPresenter implements RepaySupportContract.Presenter {

    private RepaySupportContract.View mView;

    private CompositeSubscription mSubscription;

    public RepaySupportPresenter(RepaySupportContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void findAppCnlConfBankSupportDetailList(String appCnlConfId) {
        ITradeProvider provider = PublicProviderUtil.getProvider(ITradeProvider.class);
        if (provider != null) {
            mView.showLoadingDialog(false);
            Subscription subscription = provider.findAppCnlConfBankSupportDetailList(appCnlConfId)
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<SupportBankOutput>>() {
                        @Override
                        public void onCompleted() {
                            mView.dismissLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.dismissLoadingDialog();
                            mView.showErrorText(e);
                        }

                        @Override
                        public void onNext(List<SupportBankOutput> supportBankOutputs) {
                            mView.showDataView(supportBankOutputs);
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
